package com.liuyjy.core.entity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author liuyjy
 * @Date 2022/2/7 11:31
 * @Description: 分页返回体
 **/
//@ApiModel(description = "分页返回数据封装对象", value = "分页返回数据封装对象")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class PageResult<T> implements Serializable {

    @ApiModelProperty(value = "是否存在上一页")
    public boolean hasPrevious;

    @ApiModelProperty(value = "是否存在下一页")
    public boolean hasNext;
    //@NotNull(message = "page is null ！")
    @ApiModelProperty(value = "当前页数")
    protected Long page;
    //@NotNull(message = "limit is null ！")
    @ApiModelProperty(value = "查询条数")
    protected Long limit;

    @ApiModelProperty(value = "总记录数")
    private Long total = 0L;

    @ApiModelProperty(value = "每页的数据")
    private List<T> rows = new ArrayList<>();

    @ApiModelProperty(value = "拓展信息")
    private Map<String, Object> extraInfo;

    public static <T, E> PageResult<T> getPageResult(Page<E> page, List<T> rows) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setRows(rows);
        pageResult.setHasNext(page.hasNext());
        pageResult.setHasPrevious(page.hasPrevious());
        pageResult.setTotal(page.getTotal());
        pageResult.setPage(page.getCurrent());
        pageResult.setLimit(page.getSize());
        return pageResult;
    }

    public static <T> PageResult<T> of(long total, long pageNum, long pageSize, List<T> data) {
        return of(total, pageNum, pageSize, data, null);
    }

    public static <T> PageResult<T> of(long total, List<T> data) {
        return of(total, 0, 0, data, null);
    }

    public static <T> PageResult<T> of(long total, long pageNum, long pageSize, List<T> data,
                                       Map<String, Object> extraInfo) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setRows(data);
//        pageResult.setHasNext(page.hasNext());
//        pageResult.setHasPrevious(page.hasPrevious());
        pageResult.setTotal(total);
        pageResult.setPage(pageNum);
        pageResult.setLimit(pageSize);
        pageResult.setExtraInfo(extraInfo);
        return pageResult;
    }
}
